<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\fowl\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use think\Session;
use app\fowl\model\ImsRegionsModel;
use app\fowl\model\ImsAddressModel;
use app\portal\model\PortalCategoryModel;
use app\fowl\model\ImsMeetingLiveModel;
use app\fowl\model\ImsMeetingJoinModel;

class AdminMeetingController extends AdminBaseController
{
    /**
     * 会议列表
     * @adminMenu(
     *     'name'   => '会议管理',
     *     'parent' => 'fowl/AdminMeeting/default',
     *     'remark' => '会议列表',
     * )
     */
    public function meetingList()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'compere' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.compere_name'] = $param['keyword'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            if ( isset($param['mStatus']) && $param['mStatus'] != '' ) {
                $where['vm.status'] = $param['mStatus'];
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            isset($param['mStatus']) ? $search['mStatus'] = $param['mStatus'] : '';
            Session::set('meetingList',$where);
            Session::set('meetingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('meetingList');
                $search = Session::get('meetingListKeyword'); 
            } else {
                Session::set('meetingList', null);
                Session::set('meetingListKeyword',null);
            }
        }
        // 查询列表
        $where['vm.is_vm'] = 2;
        $where['vm.is_detele'] = 0;
        $meetingModel = new ImsMeetingLiveModel();
        $meetingList = $meetingModel->meetingList($where, 'id desc', 8);

        $this->assign([
            'meetingList'       =>  $meetingList,
            'search'            => $search,
            'meetingStatus'     => meetingStatus(),
            'meetingLabel'     => meetingLabel(),
        ]);
        return $this->fetch();
    }

    /**
     * 查看会议详情
     */
    public function meetingDetails()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = $this->request->param();  
        if ( empty($param['id']) ) {
            $this->error('缺少参数', 'AdminMeeting/meetingList');exit;
        }

        // 获取会议信息
        $meetingModel = new ImsMeetingLiveModel();
        $meetingInfo = $meetingModel->meetingInfo(['vm.id'=>$param['id']]);

        $this->assign([
            'data' => $meetingInfo,
            'meetingStatus'     => meetingStatus(),
            'meetingLabel'     => meetingLabel(),
        ]);
        return $this->fetch(); 
    }

    /**
     * 查看参会人员
     */
    public function confereeLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $get = $this->request->param();
        if ( $get['vm_id'] ) {
            $where['mj.vm_id'] = $get['vm_id'];
            $where['mj.is_vm'] = 2;
        } else {
            $this->error('缺少参数');
        }

        // 获取参会人员列表
        $confereeModel = new ImsMeetingJoinModel();
        $confereeList = $confereeModel->confereeList($where);

        $this->assign([
            'confereeList' => $confereeList,
        ]);
        return $this->fetch();
    }

    /**
     * 会议地址
     */
    public function addressLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = $this->request->param();
        if ( !$param['address'] ) {
            $this->error('缺少参数');
        }

        $this->assign([
            'address' => $param['address'],
        ]);
        return $this->fetch();
    }

    /**
     * 会议回放
     */
    public function liveLook()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $get = $this->request->param();
        if ( !$get['vm_id'] ) {
            $this->error('缺少参数');
        }

        $data = Db::name('ims_meeting_live')->where('id', $get['vm_id'])->field('id, vm_title, cover_pic, store_link')->find();

        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }

    /**
     * 设置标签
     */
    public function meetingLabel()
    {
         if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['labels'] = $_POST['msg'];
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $_POST['id'])->update($data) ) {
                $this->traceLog('会议设置标签-成功', $_POST); // 操作日志
                $this->success('标签设置成功');
            } else {
                $this->traceLog('会议设置标签-失败', $_POST); // 操作日志
                $this->error('标签设置失败');
            }
        }
    }

    /**
     * 会议公开或隐藏
     */
    public function meetingOpen()
    {
         if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $_POST['msg'] == 1 ) {
                $data['is_open'] = 1;
                if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $_POST['id'])->where('is_open', 0)->where('status', 'in', [2,4,5])->update($data) ) {
                    $this->traceLog('公开会议-success', $_POST); // 操作日志
                    $this->success('公开了'.$res.'条数据');
                } else {
                    $this->traceLog('公开会议-error', $_POST); // 操作日志
                    $this->error('公开失败');
                }
            } else {
                $data['is_open'] = 0;
                if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $_POST['id'])->where('is_open', 1)->where('status', 'in', [2,4,5])->update($data) ) {
                    $this->traceLog('隐藏会议-success', $_POST); // 操作日志
                    $this->success('隐藏'.$res.'条数据');
                } else {
                    $this->traceLog('隐藏会议-error', $_POST); // 操作日志
                    $this->error('隐藏失败');
                }
            }
        }
    }

    /**
     * 会议删除
     */
    public function meetingDelete()
    {
        if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['is_detele'] = 1;
            $data['delete_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $_POST['id'])->where('is_detele', 0)->update($data) ) {
                $this->traceLog('删除会议-SUCCESS', $_POST); // 操作日志
                $this->success('成功删除'.$res.'条数据');
            } else {
                $this->traceLog('删除会议-error', $_POST); // 操作日志
                $this->error('删除失败');
            }
        }
    }

    /*****************************************************************/

    /**
     * 会议审核列表
     */
    public function meetingAuditList()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'compere' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.compere_name'] = $param['keyword'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            Session::set('meetingList',$where);
            Session::set('meetingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('meetingList');
                $search = Session::get('meetingListKeyword'); 
            } else {
                Session::set('meetingList', null);
                Session::set('meetingListKeyword',null);
            }
        }

        // 查询列表
        $where['vm.status'] = 1;
        $where['vm.is_detele'] = 0;
        $where['vm.is_vm'] = 2;
        $meetingModel = new ImsMeetingLiveModel();
        $meetingList = $meetingModel->meetingList($where, 'id desc', 8);

        $this->assign([
            'meetingList'       =>  $meetingList,
            'search'            => $search,
            'meetingStatus'     => meetingStatus(),
        ]);
        return $this->fetch();
    }

    /**
     * 会议审核
     */
    public function meetingAudit()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = $this->request->param();
        if ( $this->request->isAjax() ) {
            if ( !isset($param['id']) || !isset($param['status']) || empty($param['id']) || empty($param['status']) ) {
                $this->error('缺少参数');exit;
            }

            $data['status'] = $param['status'];
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $param['id'])->where('status', 1)->update($data) ) {
                $this->traceLog('会议审核-SUCCESS', $param); // 操作日志
                $this->success('审核完成', 'AdminMeeting/meetingAuditList');
            } else {
                $this->traceLog('会议审核-error', $param); // 操作日志
                $this->error('审核失败');
            }
        } else {
            if ( empty($param['id']) ) {
                $this->error('缺少参数', 'AdminMeeting/meetingAuditList');exit;
            }

            // 获取会议信息
            $meetingModel = new ImsMeetingLiveModel();
            $meetingInfo = $meetingModel->meetingInfo(['vm.id'=>$param['id']]);

            $this->assign([
                'data' => $meetingInfo,
            ]);
            return $this->fetch();
        }
    }

    /*****************************************************************/

    /**
     * [meetingRecycle 会议回收站]
     * @return [type] [description]
     */
    public function meetingRecycle()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $search = array();
        // 查询条件
        $param = $this->request->param();
        if ( request()->isPost() ) {
            if ( !empty($param['start_time']) && !empty($param['end_time']) ) {
                $where['vm.start_time'] = ['between',[$param['start_time'],$param['end_time']]]; // 时间区间
            }
            if ( !empty($param['keys']) && !empty($param['keyword']) ) {
                if ( $param['keys'] == 'user_id' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.user_id'] = $user_id;
                } elseif ( $param['keys'] == 'category' ) {
                    $category_id = Db::name('portal_category')->where('name', $param['keyword'])->value('id');
                    $where['vm.type_id'] = $category_id;
                } elseif ( $param['keys'] == 'compere' ) {
                    $user_id = Db::name('ims_member')->where('staff_name', $param['keyword'])->value('id');
                    $where['vm.compere_name'] = $param['keyword'];
                } else {
                    $where['vm.vm_title'] =  ['like', '%'.$param['keyword'].'%'];
                }
            }
            if ( isset($param['mStatus']) && $param['mStatus'] != '' ) {
                $where['vm.status'] = $param['mStatus'];
            }
            isset($param['start_time']) ? $search['start_time'] = $param['start_time'] : '';
            isset($param['end_time']) ? $search['end_time'] = $param['end_time'] : '';
            isset($param['keys']) ? $search['keys'] = $param['keys'] : '';
            isset($param['keyword']) ? $search['keyword'] = $param['keyword'] : '';
            isset($param['mStatus']) ? $search['mStatus'] = $param['mStatus'] : '';
            Session::set('meetingList',$where);
            Session::set('meetingListKeyword',$search);
        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('meetingList');
                $search = Session::get('meetingListKeyword'); 
            } else {
                Session::set('meetingList', null);
                Session::set('meetingListKeyword',null);
            }
        }

        // 查询列表
        $where['vm.is_detele'] = 1;
        $where['vm.is_vm'] = 2;
        $meetingModel = new ImsMeetingLiveModel();
        $meetingList = $meetingModel->meetingList($where, 'id desc', 8);

        $this->assign([
            'meetingList'       =>  $meetingList,
            'search'            => $search,
            'meetingStatus'     => meetingStatus(),
        ]);
        return $this->fetch();
    }

    /**
     * [meetingRestore 会议还原]
     * @return [type] [description]
     */
    public function meetingRestore()
    {
        $param = request()->param();
        if ( empty($param['id']) ) {
            $this->error('缺少参数');
        } else {
            $data['is_detele'] = 0;
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $param['id'])->where('is_detele', 1)->update($data) ) {
                // $this->traceLog('会议还原-SUCCESS', $param); // 操作日志
                $this->success('成功还原'.$res.'条数据');
            } else {
                // $this->traceLog('会议还原-error', $param); // 操作日志
                $this->error('还原失败');
            }
        }
    }

    /**
     * [meetingDeleteDespair 会议彻底删除]
     */
    public function meetingDeleteDespair()
    {
        $param = request()->param();
        if ( empty($param['id']) ) {
            $this->error('缺少参数');
        } else {
            $list = Db::name('ims_meeting_live')->where('id', 'in', $param['id'])->where('is_detele', 1)->field('id, cover_pic')->select()->toarray();
            foreach ( $list as $key => $val ) {
                if ( $val['cover_pic'] ) $this->delFile('./uploads/', '.'.$val['cover_pic']); // 删除图片
                Db::name('ims_meeting_join')->where('vm_id', $val['id'])->where('is_vm', 2)->delete(); // 删除参会人员
            }
            if ( $res = Db::name('ims_meeting_live')->where('id', 'in', $param['id'])->where('is_detele', 1)->delete() ) {
                // $this->traceLog('直播彻底删除-SUCCESS', $param); // 操作日志
                $this->success('成功删除了'.$res.'条数据');
            } else {
                // $this->traceLog('直播彻底删除-error', $param); // 操作日志
                $this->error('删除失败');
            }
        }
    }

    /*------------------------------------------------------------------*/

    /**
     * 会议地址列表
     */
    public function address()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别

        // 获取地址列表
        $addressModel = new ImsAddressModel();
        $list = $addressModel->lists();

        $this->assign([
            'list'          =>  $list,
        ]);
        return $this->fetch();
    }

    /**
     * 编辑地址
     */
    public function addressForm()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        if ( request()->isPost() ) {
            $post = request()->param();
            $region_id = 0;
            if(isset($post['id']) && !empty($post['id'])) $data['id'] = $post['id'];
            if ( !empty($post['provinceId']) ) {
                $region_id = $post['provinceId'];
                $data['province_id'] = $region_id;
            } 
            if ( !empty($post['cityId']) ) {
                $region_id = $post['cityId'];
                $data['city_id'] = $region_id;
            } 
            if ( !empty($post['areaId']) ) {
                $region_id = $post['areaId'];
                $data['area_id'] = $region_id;
            }

            if ( Db::name('ims_regions')->where('parent_id', $region_id)->find() ) {
                $this->error('请选择完整的地区');
            }

            $rule = [
                'address' => 'require|min:3',
                'lat' => 'require',
                'lng' => 'require',
            ];
            $msg = [
                'address.require' => '详细地址不能为空',
                'address.min' => '详细地址不能少于三位数',
                'lat'           => '请先获取准确定位',
                'lng'           => '请先获取准确定位',
            ];
            $data['address'] = input('post.address');
            $data['is_default'] = input('post.is_default');
            $data['lat'] = input('post.lat');
            $data['lng'] = input('post.lng');

            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }

            $addressModel = new ImsAddressModel();
            $res = $addressModel->set($data);
            if ( $res['code'] == 1 ) {
                $this->traceLog('编辑会议地址-SUCCESS', $post); // 操作日志
                $this->success($res['msg'], 'AdminMeeting/address');
            } else {
                $this->traceLog('编辑会议地址-error', $post); // 操作日志
                $this->error('操作失败');
            }
        } else {
            // 查询地址树
            $regionModel = new ImsRegionsModel();
            $regionList['oneRegion'] = $regionModel->regionList();

            $regionList['one'] = 0;
            $regionList['two'] = 0;
            $regionList['three'] = 0;

            // 查询编辑数据
            $data = Db::name('ims_address')->where('id',request()->param('id'))->find();
            if ( $data ) {
                $regionList['one'] = $data['province_id'];
                $regionList['two'] = $data['city_id'];
                $regionList['three'] = $data['area_id'];
                if ( !empty($data['province_id']) ) {
                    $regionList['twoRegion'][$data['province_id']] = $regionModel->regionList($data['province_id']);
                }
                if ( !empty($data['city_id']) ) {
                    $regionList['threeRegion'][$data['city_id']] = $regionModel->regionList($data['city_id']);
                }

                // 获取地址
                $province = Db::name('ims_regions')->where('region_id',$data['province_id'])->value('region_name');
                $city = Db::name('ims_regions')->where('region_id',$data['city_id'])->value('region_name');
                $area = Db::name('ims_regions')->where('region_id',$data['area_id'])->value('region_name');
                $ads = '中国'.$province.$city.$area.$data['address'];
            }

            $this->assign([
                'regionList'    => $regionList,
                'data'          => $data,
                'address'       => $ads,
            ]);
            
            return $this->fetch();
        }
    }

    /**
     * 地址地区请求
     */
    public function getSelect()
    {
        $parentId = request()->param('parentid');
        $list = DB::name('ims_regions')->where('parent_id', $parentId)->field('region_id, region_name, parent_id')->select()->toArray();
        $this->success('ok','', $list);
    }

    /**
     * 根据地址获取经纬度
     */
    public function getLatLng()
    {
        $param = request()->param();
        if ( empty($param) ) {
            $this->error('缺少参数');
        }
        $province = Db::name('ims_regions')->where('region_id',$param['provinceId'])->value('region_name');
        $city = Db::name('ims_regions')->where('region_id',$param['cityId'])->value('region_name');
        $area = Db::name('ims_regions')->where('region_id',$param['areaId'])->value('region_name');
        $address = '中国'.$province.$city.$area.$param['address'];

        if ( $province && $city && $address) {
            $this->success($address);
        } else {
            $this->error('地址获取失败！');
        }
    }

    /**
     * 地址删除
     */
    public function addressDelete()
    {
        if ( empty($_POST['id']) ) {
            $this->error('缺少参数');
        } else {
            if ( Db::name('ims_address')->delete($_POST['id']) ) {
                $this->success('数据已删除');
            } else {
                $this->error('删除失败');
            }
        }
    }
}
